package com.zb.rebbitMq;

import java.io.IOException;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

public class ConsumerEmail {
	
	private static final String QUEUE_INFORM_EMAIL ="queue_inform_email";
	private static final String QUEUE_INFORM_SMS ="queue_inform_sms";
	private static final String EXCHANGE_FANOUT_INFORM ="exchange_fanout_inform";
	private static final ConnectionFactory factory = new ConnectionFactory();
	public ConsumerEmail() {
		
		factory.setHost("106.12.222.85");
		factory.setPort(5672);
		factory.setUsername("guest");
		factory.setPassword("guest");
		factory.setVirtualHost("/");
	}
	
	public void recevice() {
		try {
		 Connection  connection = factory.newConnection();
		 Channel  channel = connection.createChannel();
			
			//申明队列
			channel.queueDeclare(QUEUE_INFORM_EMAIL, false, false, false, null);
			
			//申明交换机
			channel.exchangeDeclare(EXCHANGE_FANOUT_INFORM, BuiltinExchangeType.FANOUT);
			
			channel.queueBind(QUEUE_INFORM_EMAIL, EXCHANGE_FANOUT_INFORM, "");
			
		
			DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {

				@Override
				public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
						throws IOException {
					System.out.println(consumerTag);
					System.out.println(envelope.getExchange());
					String message =new String(body,"utf-8");
					System.out.println("【recevice】"+message);
				}
				
			}; 
			
			channel.basicConsume(QUEUE_INFORM_EMAIL, true,defaultConsumer);
			
	}catch (Exception e) {
		// TODO: handle exception
	}
	}
}
